/**
 * @projectName demo-master
 * @fileName NacosRegisterConfiguration.java
 * @packageName org.example.nacos.dicsovery.config
 * @author xaoyaoyao
 * @date 2021/3/30 09:19
 * @version V1.0
 * @copyright (c) 2021, xaoyaoyao@aliyun.com All Rights Reserved.
 */
package org.example.nacos.dicsovery.config;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @author xaoyaoyao
 * @className NacosRegisterConfiguration
 * @description
 * @date 2021/3/30 09:19
 */
@Configuration
public class NacosRegisterConfiguration {

	@Value("${server.host}")
	private String host;

	@Value("${server.port}")
	private int serverPort;

	@Value("${server.clusterName}")
	private String clusterName;

	@Value("${spring.application.name}")
	private String applicationName;

	@Value("${spring.application.groupName}")
	private String groupName;

	@NacosInjected
	private NamingService namingService;

	@PostConstruct
	public void registerInstance() throws NacosException {
		clusterName = StringUtils.isNoneBlank(groupName) ? clusterName : "DEFAULT";
		host = StringUtils.isNoneBlank(host) ? host : "127.0.0.1";
		if (StringUtils.isNoneBlank(groupName)) {
			namingService.registerInstance(applicationName, groupName, host, serverPort, clusterName);
			return;
		}
		namingService.registerInstance(applicationName, host, serverPort, clusterName);
	}
}
